package com.ftg.learn.chapter15;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author kangjx
 * @createTime 2022/8/24
 */
public class Question {

    public static void main(String[] args) {

        List<Cat> list = new ArrayList<>(5);
        Cat c1 = new Cat();
        c1.setSno(1);
        c1.setName("tomcat");
        list.add(c1);

        c1 = new Cat();
        c1.setSno(4);
        c1.setName("duck");
        list.add(c1);

        c1 = new Cat();
        c1.setSno(2);
        c1.setName("tom");
        list.add(c1);

        c1 = new Cat();
        c1.setSno(3);
        c1.setName("jerrry");
        list.add(c1);

        c1 = new Cat();
        c1.setSno(3);
        c1.setName("jerrry");
        list.add(c1);
    //c - T - list->>cat  c.getName()  R list - cat - getName
        List<String> catNames = list.stream()
                //过滤掉猫名为tom的
//                .filter(c->!c.getName().equals("tom"))
//                .map(c->c.getName())
                .map(Cat::getName)
//                .mapToInt(Cat::getSno)
//                .sorted()
//                .boxed()
                .collect(Collectors.toList());
        System.out.println(catNames+"：猫编号");

        int sum = list.stream()
                .mapToInt(Cat::getSno).sum();
        System.out.println("猫的编号总和:" + sum);
        OptionalDouble avg = list.stream()
                .mapToInt(Cat::getSno).average();
        System.out.println("猫的编号平均值:" + avg.getAsDouble());

        List<Cat> list2 = list.stream()//.filter(x -> x.getName().startsWith("t"))
                .skip(0)
                .limit(3)
                .collect(Collectors.toList());
        System.out.println(list2);

        Optional<Cat> op = list.stream()
                .findAny();
        Cat c = op.get();
        System.out.println("当前取出来的猫:"+c);

        List<Cat> list3 = list.stream().peek(t->{
            if(t.getName().startsWith("t")){
               t.setName(new StringBuilder(t.getName()).append(" bad cat ").toString());
            }
        }).collect(Collectors.toList());

        System.out.println(list3+"==> peek");
        boolean ist = list.stream().allMatch(s->s.getSno() > 2);
        System.out.println(ist + " allMatch判断当前stream流里面条件是否满足");

        Map<Integer,String> map = list.stream()

                .distinct().filter(x->x.getSno()==4)

                .collect(Collectors.toConcurrentMap(Cat::getSno,Cat::getName));
        System.out.println(map);


//        Collectors.toMap()

//        int[] as = new int[]{1,2,2,3,1,23,2,23,123,123,123,123};
//        Arrays.asList(as).stream().distinct().collect(Collectors.toList()).forEach(System.out::println);

//        System.out.println();
        //猜测的是 ： as 0
//        int[] newas = Arrays.stream(as).distinct().toArray();
//        System.out.println(Arrays.toString(newas));
//        System.out.println();
//        System.out.println(Stream.of(1, 2, 2, 3, 1, 23, 2, 23, 123, 123, 123, 123).distinct().collect(Collectors.toList()));

//
//        Map<String,String> map = new HashMap<>(2);
//        map.put("name","张三");
//        map.put("sex","男");
//        Set<Map.Entry<String,String>> entrys = map.entrySet();
//        for (Map.Entry<String,String> sets :entrys) {
//                        sets.getValue()
//            List<String> strs = Stream.of("23","56").collect(Collectors.toList());
//        }
/*
        Collection<String> strs = map.values();
        System.out.println(Stream.of(map).filter(x->x.).collect(Collectors.toList()));*/

//        System.out.println(map.values());




//        Collections.sort(list, Comparator.comparingInt(Cat::getSno));
//        System.out.println(list);

//        for (Cat cat : list) {
//            if(cat.getSno() >3){
//                System.out.println(cat.getName());
//            }
//        }

//        Map<String,String> map = new HashMap<>();
//        map.put("name","aa");
//        map.put("name","bb");
//        Map<String,String> map1 = Stream.of(map)
//
//                .filter(x->x.get("name").startsWith("a"))
//
//                .collect(Collectors.toMap(k->k.toString(),v->v.toString()));
//        System.out.println(map1);
    }

}
